﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Inspired.Euler
{
    public static class Problem038
    {
        /// <summary>
        /// What is the largest 1 to 9 pandigital that can be formed by multiplying a fixed number by 1, 2, 3, ... ?
        /// </summary>
        [EulerProblem(38, Title = "What is the largest 1 to 9 pandigital that can be formed by multiplying a fixed number by 1, 2, 3, ... ?")]
        public static long Solve()
        {
            long largest = 0;
            long max = 987654321;
            StringBuilder number = new StringBuilder();
            for (long candidate = 9; candidate <= (max / 2); candidate++)
            {
                Console.Title = String.Format("{0:0,000} - {1}", candidate, largest);

                number.Clear();

                int digit = 1;
                while (number.Length < 9)
                    number.Append((candidate * digit++).ToString());

                if(number.Length > 9)
                    continue;

                long value = Int64.Parse(number.ToString());
                if (value > largest && value.AsEnumerable().IsPandigital())
                    largest = value;

                //List<long> product = new List<long>();
                //for (int digit = 1; ; digit++)
                //{
                //    product.AddRange((candidate * digit).AsEnumerable().Reverse());

                //    if (product.Count < 9)
                //        continue;

                //    if (product.Count > 9)
                //        break;

                //    //if (value > max)
                //    //    break;

                //    long value = product.ToInt64();

                //    if (value > largest && 
                //        //value >= 900000000 && value <= 987654321 &&
                //        product.AsEnumerable().IsPandigital())
                //    {
                //        largest = value;
                //        break;
                //    }
                //}
            }

            return largest;
        }
    }
}
